package io.renren.controller;

import java.math.BigDecimal;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.stereotype.Controller;

import io.renren.entity.DataContractpayDetailEntity;
import io.renren.entity.DataPayrollEntity;
import io.renren.entity.xtd.InformalCheck;
import io.renren.service.DataContractpayDetailService;
import io.renren.service.DataPayrollService;
import io.renren.service.xtd.DataContractpayDetailServiceXtd;
import io.renren.utils.PageUtils;
import io.renren.utils.R;


/**
 * 合同分批明细表
 * 
 * @author chenshun
 * @email sunlightcs@gmail.com
 * @date 2018-05-31 13:49:46
 */
@Controller
@RequestMapping("informalCheck")
public class InformalCheckController {
	@Autowired
	private DataContractpayDetailServiceXtd dataContractpayDetailServiceXtd;
	@Autowired
	private DataPayrollService dataPayrollService;
	@RequestMapping("/informalcheck.html")
	public String list(){
		return "informalCheck/informalcheck.html";
	}

	/**
	 * 列表
	 */
	@ResponseBody
	@RequestMapping("/list")
	public R list(Integer page, Integer limit,String value){
		Map<String, Object> map = new HashMap<>();
		map.put("offset", (page - 1) * limit);
		map.put("limit", limit);
		map.put("value", value);
		//查询列表数据
		List<InformalCheck> informalChecklList = dataContractpayDetailServiceXtd.getInfoOfInformal(map);
		for (InformalCheck informalCheck : informalChecklList) {
			String employeeID = informalCheck.getEmployeeID();
			DataPayrollEntity newRecord = dataPayrollService.getNewRecord();
			if(newRecord!=null) {
				String payrollmonth = newRecord.getPayrollmonth();
				String month=payrollmonth.replace("-", "");
				int thisMonth = Integer.parseInt(month);
				int lastMonth=0;
				Integer workMonth=0;
				//如果是一月份，则算到去年12月份
				if((thisMonth-1)%100==0) {
					lastMonth=thisMonth-100+11;
				}else {
					lastMonth=thisMonth-1;
				}
				Map<String, Object> mapp = new HashMap<>();
				mapp.put("month", String.valueOf(thisMonth));
				mapp.put("employeeID", employeeID);
				BigDecimal allAchi = dataContractpayDetailServiceXtd.getAllAchi(mapp);
				if(allAchi==null) {
					allAchi=new BigDecimal(0);
				}
				Map<String, Object> mapp2 = new HashMap<>();
				mapp2.put("month", String.valueOf(lastMonth));
				mapp2.put("employeeID", employeeID);
				BigDecimal allAchi2 = dataContractpayDetailServiceXtd.getAllAchi(mapp2);
				if(allAchi2==null) {
					allAchi2=new BigDecimal(0);
				}
				BigDecimal allachi=allAchi.add(allAchi2);
				informalCheck.setTwoMonthMoney(allachi);
				Date inOrgTime = informalCheck.getInOrgTime();
				SimpleDateFormat sdf=new SimpleDateFormat("yyyyMMdd");
				String format = sdf.format(inOrgTime);
				Integer a=Integer.parseInt(format);
				Integer b=thisMonth*100;
				if(a%100<=15) {
					//此月算工作月
					if((a/10000)<(b/10000)) {
						//跨年
						workMonth=b/100%100-a/100%100+12*(b/10000-a/10000)+1;
					}else {
						workMonth=b/100-a/100+1;
					}
				}else {
					if((a/10000)<(b/10000)) {
						//跨年
						workMonth=b/100%100-a/100%100+12*(b/10000-a/10000);
					}else {
						workMonth=b/100-a/100;
					}
				}
				informalCheck.setWorkTime(workMonth);
			}
			
			}
			
		int total = dataContractpayDetailServiceXtd.getInformalCount(map);

		PageUtils pageUtil = new PageUtils(informalChecklList, total, limit, page);

		return R.ok().put("page", pageUtil);
	}



}
